package com.example.testservice;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


//	 @Override
//	  public void onCreate(Bundle savedInstanceState) {
//	    super.onCreate(savedInstanceState);
//	    setContentView(R.layout.activity_main);
//	    textView = (TextView) findViewById(R.id.status);
//	    
//
//	  }
//
//	private TextView textView;
//	  private BroadcastReceiver receiver = new BroadcastReceiver() {
//
//	    @Override
//	    public void onReceive(Context context, Intent intent) {
//	      Bundle bundle = intent.getExtras();
//	      if (bundle != null) {
//	        String string = bundle.getString(DownloadService.FILEPATH);
//	        int resultCode = bundle.getInt(DownloadService.RESULT);
//	        if (resultCode == RESULT_OK) {
//	          Toast.makeText(MainActivity.this,
//	              "Download complete. Download URI: " + string,
//	              Toast.LENGTH_LONG).show();
//	          textView.setText("Download done");
//	        } else {
//	          Toast.makeText(MainActivity.this, "Download failed",
//	              Toast.LENGTH_LONG).show();
//	          textView.setText("Download failed");
//	        }
//	      }
//	    }
//	  };
//
//	  @Override
//	  protected void onResume() {
//	    super.onResume();
//	    registerReceiver(receiver, new IntentFilter(DownloadService.NOTIFICATION));
//	  }
//	  @Override
//	  protected void onPause() {
//	    super.onPause();
//	    unregisterReceiver(receiver);
//	  }
//
//	  public void onClick(View view) {
//
//	    Intent intent = new Intent(this, DownloadService.class);
//	    // add infos for the service which file to download and where to store
//	    intent.putExtra(DownloadService.FILENAME, "index.html");
//	    intent.putExtra(DownloadService.URL,
//	        "http://www.vogella.com/index.html");
//	    startService(intent);
//	    textView.setText("Service started");
//	  }
//
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}
	  private TextView textView;
	 @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	    textView = (TextView) findViewById(R.id.TextView01);
	  }

	  private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
	    @Override
	    protected String doInBackground(String... urls) {
	      String response = "";
	      for (String url : urls) {
	        DefaultHttpClient client = new DefaultHttpClient();
	        HttpGet httpGet = new HttpGet(url);
	        try {
	          HttpResponse execute = client.execute(httpGet);
	          InputStream content = execute.getEntity().getContent();

	          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
	          String s = "";
	          while ((s = buffer.readLine()) != null) {
	            response += s;
	          }

	        } catch (Exception e) {
	          e.printStackTrace();
	        }
	      }
	      return response;
	    }

	    @Override
	    protected void onPostExecute(String result) {
	      textView.setText(result);
	    }
	  }

	  public void onClick(View view) {
	    DownloadWebPageTask task = new DownloadWebPageTask();
	    task.execute(new String[] { "http://www.vogella.com" });

	  }

}
